package com.cze.dao.impl;

import com.cze.dao.ProductDAO;
import com.cze.vo.Product;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class ProductDAOImpl implements ProductDAO {
    private JdbcTemplate jdbcTemplate;

    @Resource(name = "jdbcTemplate")
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int add(Product product) {
        Object[] obj = new Object[]{product.getSerialNumber(),product.getName(),product.getBrand(),product.getModel(),product.getPrice(),product.getPicture(),product.getDescription()};
        return jdbcTemplate.update("insert into product(serialNumber, name, brand, model, price, picture, description) VALUES (?, ?, ?, ?, ?, ?, ?)",
                                    obj);
    }

    @Override
    public int delelte(int productID) {
        return jdbcTemplate.update("delete from product where productID=?",
                                    productID);
    }

    @Override
    public int update(Product product) {
        Object[] obj = new Object[]{product.getSerialNumber(),product.getName(),product.getBrand(),product.getModel(),product.getPrice(),product.getPicture(),product.getDescription(),product.getProductID()};
        return jdbcTemplate.update("update product set serialNumber=?, name=?, brand=?, model=?, price=?, picture=?, description=? where productID=?",
                                    obj);
    }


    @Override
    public List<Product> selectAll() {
        return jdbcTemplate.query("select * from product",
                                    new BeanPropertyRowMapper<Product>(Product.class));
    }

    @Override
    public Integer countAll(){
        return jdbcTemplate.queryForObject("select count(*) from product",
                                            Integer.class);
    }

    @Override
    public List<Product> selectPage(int page, int limit) {
        Object[] obj = new Object[]{page*limit, limit};
        return jdbcTemplate.query("select * from product limit ?,?",
                                    new BeanPropertyRowMapper<Product>(Product.class),
                                    obj);
    }

    @Override
    public List<Product> findByKeyWord(String keyword) {
        Object[] obj = new Object[]{"%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%", "%"+keyword+"%"};
        return jdbcTemplate.query("select * from product where serialNumber like ? or name like ? or brand like ? or model like ? or description like ?",
                                    new BeanPropertyRowMapper<Product>(Product.class),
                                    obj);
    }

    @Override
    public Product findByID(int productID) {
        try {
            return jdbcTemplate.queryForObject("select * from product where productID=?",
                                                new BeanPropertyRowMapper<Product>(Product.class),
                                                productID);
        }catch (EmptyResultDataAccessException e){
            System.out.println(e.getMessage());
            return null;
        }
    }
}
